/*
 *
 * @time 2020/12/12  15:35
 *
 * @desc EventBus帮助类
 *
 */

import 'dart:async';
import 'package:event_bus/event_bus.dart';

class MyEventBusHelp {
  //标识
  String tag = "";
  //数据
  dynamic result;

  MyEventBusHelp(this.tag, {this.result});
}

/*
 *
 * @time 2020/12/12  15:15
 *
 * @desc EventBus工具类
 *
 */
class MyEventBusUtil {
  late EventBus eventBus;
  static final MyEventBusUtil _singleton = MyEventBusUtil._internal();

  MyEventBusUtil._internal() {
    eventBus = EventBus();
    // 初始化代码...
  }

  factory MyEventBusUtil() {
    return _singleton;
  }

  //获取单例
  static EventBus getEventBus() {
    return _singleton.eventBus;
  }

  //返回某事件的订阅者
  // ignore: non_constant_identifier_names
  static StreamSubscription<MyEventBusHelp> onEventResult<MyEventBusHelp>(Function(MyEventBusHelp) onData) {
    //内部流属于广播模式，可以有多个订阅者
    return MyEventBusUtil.getEventBus().on<MyEventBusHelp>().listen(onData);
  }

  //发送事件
  static void sentEvent(MyEventBusHelp e) {
    MyEventBusUtil.getEventBus().fire(e);
  }

  //返回某事件的订阅者
  static StreamSubscription<T> listen<T>(Function(T event) onData) {
    //内部流属于广播模式，可以有多个订阅者
    return MyEventBusUtil.getEventBus().on<T>().listen(onData);
  }

  //发送事件
  static void sentEventT<T>(T e) {
    MyEventBusUtil.getEventBus().fire(e);
  }
}

/*
 *
 * @time 2020/12/12  15:35
 *
 * @desc EventBus标识
 *
 */

class MyEventBusTag {
  //message
  //链接状态的改变
  static const String onConnectionStatusChanged = "onConnectionStatusChanged";

  //接收到信息
  static const String onMessageReceived = "onMessageReceived";

  //定位成功
  static const String locationSuccess = "locationSuccess";

  //刷新订单
  static const String refreshOrderList = "refreshOrderList";
}
